<?php
/**
 * 公共方法
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class FunctionController extends Controller
{
    public static function password($value) {
        return Hash::make($value);
    }

    /**
     * 无限极分类生成层级数组
     * @param $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @return array
     */
    public static function generate_tree($list, $pk = 'id', $pid = 'parent_id', $child = 'child')
    {
        $items = [];
        foreach ($list as $val) $items[$val[$pk]] = $val; //id值必须和它对应的键值一致
        foreach ($items as $item)
            $items[$item[$pid]][$child][$item[$pk]] = &$items[$item[$pk]];
        return isset($items[0][$child]) ? $items[0][$child] : [];
    }
}
